package com.libertas.begin;

import com.libertas.begin.R;
import com.libertas.interfaces.IJumpTable;
import com.libertas.interfaces.IMenu;
import com.libertas.jump.MenuFirstLoginJump;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {
	private ListView menuView;

	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.main);
		menuView=(ListView)findViewById(R.id.mainMenu);
		menuView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int item,
					long arg3) {
				
				String selectedItem = (String) menuView.getItemAtPosition(item);

				if(selectedItem.equals(HomeLandSecurity.getStringResourse(R.string.exit_name))){
						// if user want to clear the login status
						HomeLandSecurity.setJumpTable(MenuFirstLoginJump.getInstance());
						MainActivity.this.finish();
				}
				else if(selectedItem.equals(HomeLandSecurity.getStringResourse(R.string.sign_out))){
						IJumpTable jmt = MenuFirstLoginJump.getInstance();
						HomeLandSecurity.setJumpTable(jmt);
						HomeLandSecurity.setUsername("");
						HomeLandSecurity.setPassword("");
						String[] menulist = ((IMenu)jmt).getMenuDescriptor();
					    menuView.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1 , menulist));
				}
				else{
					IJumpTable jmp = HomeLandSecurity.getJumpTable();
					Class cls = jmp.getActivityClass(selectedItem);
					if(cls != null){
					Intent intent = new Intent();
			        intent.setClass(MainActivity.this, cls);
			        startActivity(intent);
			        }
				}
			}
			});
	 
		}
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
       String[] menulist = ((IMenu)(HomeLandSecurity.getJumpTable())).getMenuDescriptor();
       menuView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , menulist));
    }

}